package com.crm.redis.service.user.impl;

import com.crm.common.util.SendEamil;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.vo.user.UserAccountManagerVO;
import com.crm.redis.RedisPrefix;
import com.crm.redis.mapper.ValueRMapper;
import com.crm.redis.service.user.UserAccountRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @Author:chaifuyou
 * @Date: 2021/6/18 8:33
 */
@Service
public class UserAccountRedisRedisServiceImpl implements UserAccountRedisService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 明文密码存redis
     * @param newUserAccount
     * @param password
     */
    @Override
    public boolean addUserAccountPassword(UserAccount newUserAccount, String password) {
        String key = RedisPrefix.USER_ACCOUNT_PASSWORD + newUserAccount.getAccountId();
        boolean result = ValueRMapper.setRedisString(redisTemplate, key,password);
        return result;
    }

    /**
     * 发送邮件告诉用户密码
     * @param list
     */
    @Override
    public void sendUserPasswordToMail(List<UserAccount> list) {
        for (int i = 0;i<list.size();i++){
            String key = RedisPrefix.USER_ACCOUNT_PASSWORD + list.get(i).getAccountId();
            String password = ValueRMapper.getRedisString(redisTemplate, key);
            String sendTitle = "关于crm系统账号创建说明";
            String sendText = "您的crm系统账号创建成功，账号："+list.get(i).getUserAccount()+" ;密码："+password;
            SendEamil.sendEamil(sendTitle,sendText,list.get(i).getUserAccount());
        }
    }

    /**
     * 把离职用户密码发送到邮箱
     * @param list
     * @param sendMail
     */
    @Override
    public void sendLeaveUserPasswordToMail(List<UserAccountManagerVO> list, String sendMail) {
        String sendTitle = "关于离职员工crm系统账号创建账号通知";
        String sendText = "";
        for (int i=0;i<list.size();i++){
            String key = RedisPrefix.USER_ACCOUNT_PASSWORD + list.get(i).getAccountId();
            String password = ValueRMapper.getRedisString(redisTemplate, key);
            if (Objects.nonNull(password) && !"".equals(password)){
                sendText += ("员工名称："+list.get(i).getUserName()+"，账号为："+list.get(i).getUserAccount()+",密码为："+password+",\r\n登录网址:https://www.dcrm.cn/login;");
            }
        }
        if (!"".equals(sendText)){
            SendEamil.sendEamil(sendTitle,sendText,sendMail);
        }
    }


    @Override
    public Boolean addUserAccount(List<UserAccount> list) {
        boolean result = false;
        for(UserAccount userAccount : list) {
            String key = RedisPrefix.USER_ACCOUNT + userAccount.getUserAccount();
            result = ValueRMapper.setRedisObject(redisTemplate, key,userAccount);
        }
        return result;


    }

    /***
     * @Description:根据账号id获取密码
     * @Param: userAccountId 账号id
     * @Author: luojie
     * @Date: 2021/7/18 14:35
     */
    @Override
    public String selectUserAccountPassword(String userAccountId) {
        String key = RedisPrefix.USER_ACCOUNT_PASSWORD +userAccountId;
        String result = ValueRMapper.getRedisString(redisTemplate,key);
        return result;
    }

    /**
     * 用户表新增增加redis数据
     * @param user
     * @return
     */
    @Override
    public Boolean addRedisUser(UserAccount user) {
        String key = RedisPrefix.USER_ACCOUNT + user.getUserAccount();
        return ValueRMapper.setRedisObject(redisTemplate, key,user);
    }
}
